package top.iflyoung.cim.route.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import top.iflyoung.cim.common.facade.BaseChatFacade;
import top.iflyoung.cim.common.req.GroupMessageVo;
import top.iflyoung.cim.common.req.P2PMessageVo;
import top.iflyoung.cim.route.translator.MessageTranslator;

import java.util.ArrayList;
import java.util.Set;

/**
 * @author liguiyang
 * @description 聊天任务
 * @sine 1.8
 * @date 2021/9/621:44
 **/
@Configuration
public class ChatMessageSyncTask {

    @Autowired
    private MessageTranslator messageTranslator;

    @Autowired
    @Qualifier("sqlChatFacadeService")
    private BaseChatFacade baseChatFacade;

    /**
     * 同步分组聊天记录
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void syncGroupMessage() {
        Set<GroupMessageVo> groupMessageVos = messageTranslator.parseGroupFromRedisAtYesterday();
        baseChatFacade.saveGroupMessages(new ArrayList<>(groupMessageVos));
    }

    /**
     * 同步私聊信息
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void syncP2PMessage() {
        Set<P2PMessageVo> p2PMessageVos = messageTranslator.parseP2PMessageFromRedisAtYesterday();
        baseChatFacade.saveP2PMessages(new ArrayList<>(p2PMessageVos));
    }

}
